/*
* Copyright (C) 2004-2010  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

#include "stdafx.h"
#include "FdoCMSXmlGlobals.h"
#include "FdoCMSGoogleGetMap.h"
#include <FdoCommonStringUtil.h>
#include <OWS/FdoOwsGlobals.h>

FdoCMSGoogleGetMap::FdoCMSGoogleGetMap () :   
m_dCenterLat(0.0), 
m_dCenterLon(0.0), 
m_iZoomLevel(0), 
m_Height(0),
m_Width(0),
m_bSensor(false)
{
}

FdoCMSGoogleGetMap::FdoCMSGoogleGetMap (FdoDouble dCenterLat, 
                                        FdoDouble dCenterLon,
                                        FdoInt32 iZoomLevel,
                                        FdoSize height,
                                        FdoSize width,
                                        FdoString* imageFormat,
                                        FdoString* mapType,
                                        FdoString* apiKey,
                                        FdoBoolean bSensor) 
                                        : m_dCenterLat(dCenterLat), 
                                        m_dCenterLon(dCenterLon),
                                        m_iZoomLevel(iZoomLevel),
                                        m_Height(height),
                                        m_Width(width), 
                                        m_Format(imageFormat ? imageFormat : L""), 
                                        m_MapType(mapType ? mapType : L""), 
                                        m_APIKey(apiKey ? apiKey : L""),
                                        m_bSensor(bSensor)
{
}

FdoCMSGoogleGetMap* FdoCMSGoogleGetMap::Create (FdoDouble dCenterLat, 
                                                FdoDouble dCenterLon,
                                                FdoInt32 iZoomLevel,
                                                FdoSize height,
                                                FdoSize width,
                                                FdoString* imageFormat,
                                                FdoString* mapType,
                                                FdoString* apiKey,
                                                FdoBoolean bSensor) 
{
    return new FdoCMSGoogleGetMap (dCenterLat, dCenterLon, iZoomLevel, height, width, imageFormat, mapType, apiKey, bSensor);
}

FdoCMSGoogleGetMap::~FdoCMSGoogleGetMap ()
{
}

FdoStringP FdoCMSGoogleGetMap::EncodeKVP()
{
    // For common request, version and service
    FdoStringP ret = L"";

    // Add "center" parameters in the request    
    ret += FdoCMSXmlGlobals::CMSGoogleRequestCenter;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%lf", m_dCenterLat);
    ret += FdoCMSXmlGlobals::CMSRequestComma;
    ret += FdoStringP::Format(L"%lf", m_dCenterLon);

    // Add "zoom" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSGoogleRequestZoom;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", m_iZoomLevel);

    // Add "size" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSGoogleRequestSize;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", m_Width);
    ret += FdoCMSXmlGlobals::CMSRequestMul;
    ret += FdoStringP::Format(L"%d", m_Height);

    // Add "format" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSGoogleRequestFormat;
    ret += FdoOwsGlobals::Equal;
    ret += m_Format;

    // Add "mapType" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSGoogleRequestMapType;
    ret += FdoOwsGlobals::Equal;
    ret += m_MapType;

    // Add "key" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSGoogleRequestAPIKey;
    ret += FdoOwsGlobals::Equal;
    ret += m_APIKey;

    // Add "sensor" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSGoogleRequestSensor;
    ret += FdoOwsGlobals::Equal;
    if (m_bSensor)
    {
        ret += FdoCMSXmlGlobals::CMSRequestTransparentTrue;
    }
    else
    {
        ret += FdoCMSXmlGlobals::CMSRequestTransparentFalse;
    }

    // Translate to a valid HTTP request
    ret = ret.Replace(L" ", L"%20"); 
    //ret = L"center=40.714728,-73.998672&zoom=14&size=400x400&sensor=false&format=gif";
    return ret;
}

FdoStringP FdoCMSGoogleGetMap::EncodeXml()
{
    // TODO: shall we support this?
    return L"";
}
